package com.bilibili.movies.util;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.Random;


@Data
@Component
public class EmailUtils {
    private static Random rand=new Random();
    @Autowired
    private  JavaMailSender javaMailSender;

    public String sendMessage(String email) throws MessagingException, UnsupportedEncodingException {

        // 创建一个邮件消息
        MimeMessage message = this.javaMailSender.createMimeMessage();

        // 创建 MimeMessageHelper
        MimeMessageHelper helper = new MimeMessageHelper(message, false);

        // 发件人邮箱和名称
        helper.setFrom("3388659470@qq.com", "luobinyu");
        // 收件人邮箱
        helper.setTo(email);
        // 邮件标题
        helper.setSubject("来自罗彬玉大帅比的验证码，收好！！！");
        //生成验证码
        String result= String.valueOf(rand.nextLong(10000,99999));
        // 邮件正文，第二个参数表示是否是HTML正文
        helper.setText(result, false);

        // 发送
        javaMailSender.send(message);
        return result;
    }
}